﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Library;
using QLKH.PopUp;

namespace QLKH.PopUp.taodulieu
{
    public partial class NhapSoDHTheoMaKH : Form
    {
        DataGridView data;
        private SqlConnection sqlcon;
        //private SqlCommand sqlcmd;
        public NhapSoDHTheoMaKH(DataGridView dt)
        {
            InitializeComponent();
            data = dt;
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btntinhcuoc_Click(object sender, EventArgs e)
        {
            clTaoTbSuDung tbs = new clTaoTbSuDung();
            string makh = txtMaKH.Text.Trim();
            Int16 somoi = Int16.Parse(txtchisomoi.Text.Trim());
            string tbsudung = UpdateData.thangnamload;
            Int64 idkh = 0;
           
            if (makh !="")
            {
                try
                {
                    using (sqlcon = clHoadon.GetConnection())
                    {
                        sqlcon.Open();
                        
                        SqlCommand command = new SqlCommand("select id from tbKhachHang where maCu='" +makh +"' and trangthai=3", sqlcon);
                        SqlDataReader reader = command.ExecuteReader();
                        //datagridTBSuDung.Rows[int.Parse(datagridTBSuDung.CurrentRow.Index.ToString())].Cells["tongtien"].Value = int.Parse(datagridTBSuDung.Rows[int.Parse(datagridTBSuDung.CurrentRow.Index.ToString())].Cells["idkh"].Value.ToString());
                        while (reader.Read())
                        {
                            idkh = Int64.Parse(reader["id"].ToString());
                        }
                        tbs.tinhcuoc(idkh, somoi, tbsudung);

                        sqlcon.Close();
                    }
                }
                catch { }
                finally { sqlcon.Dispose(); }

                MessageBox.Show("Đã tính cước xong cho khách hàng "+makh);
                txtMaKH.Text = "";
                txtchisomoi.Text = "";
                //txtMaKH.Focus();
            }
            else {
                MessageBox.Show("Mã khách hàng này không có");
            }
        }

        private void txtMaKH_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                txtchisomoi.Focus();
            }
        }

        private void txtchisomoi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.Send("{enter}");
                this.AcceptButton = btntinhcuoc;
            }
        }

        private void NhapSoDHTheoMaKH_Load(object sender, EventArgs e)
        {
            clKhachhang clkh = new clKhachhang();
            AutoCompleteStringCollection dskh = new AutoCompleteStringCollection();

            DataTable dtds = clkh.getautocomplete(0, 0);
            if (dtds != null && dtds.Rows.Count > 0)
            {
                foreach (DataRow row in dtds.Rows)
                {
                    dskh.Add(row["maCu"].ToString());
                }
            }

            txtMaKH.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtMaKH.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtMaKH.AutoCompleteCustomSource = dskh;
        }

        private void btntinhcuoc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                txtMaKH.Focus();
            }
        }
    }
}